package com.azzcs.customer.dao.impl;


import com.azzcs.customer.dao.TransferDao;
import com.azzcs.customer.entity.Account;
import com.azzcs.customer.utils.ConnectionUtils;
import com.azzcs.spring.anno.Autowired;
import com.azzcs.spring.anno.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author: wzg
 * @Date: 2020/12/13 下午4:18
 */
@Repository("transferDao")
public class TransferDaoImpl implements TransferDao {


    @Autowired
    private ConnectionUtils connectionUtils;


    @Override
    public Account queryAccountByCardNo(String cardNo) throws Exception {
        Connection connection = connectionUtils.getConnection();
        String sql = "SELECT id,card_no,money FROM account WHERE card_no = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, cardNo);
        statement.executeQuery();
        ResultSet resultSet = statement.getResultSet();
        while (resultSet.next()) {
            long id = resultSet.getLong("id");
            String card_no = resultSet.getString("card_no");
            Integer money = resultSet.getInt("money");
            return new Account(id, card_no, money);
        }
        return null;
    }

    @Override
    public int updateAccountByCardNo(Account account) throws Exception {
        Connection connection = connectionUtils.getConnection();
        String sql = "UPDATE account SET money = ? WHERE card_no = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, account.getMoney());
        statement.setString(2, account.getCardNo());
        statement.execute();

        return statement.getUpdateCount();
    }
}
